package controllers.util;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import controllers.BaseController;
import play.mvc.Http;
import play.mvc.Result;
import services.ALiYunService;
import java.util.Date;

/**
 * Created by Administrator on 2015/6/19.
 */
public class UeditorController extends BaseController {

    //后端配置参数设置
    public static Result ueditorServer(){
        JSONObject object = new JSONObject();
        object.put("imageActionName","uploadimage");
        JSONArray AllowFiles = new JSONArray();
        AllowFiles.add(".png");
        AllowFiles.add(".jpg");
        object.put("imageAllowFiles",AllowFiles);
        object.put("imageFieldName","upfile");
        object.put("imageMaxSize",102400);
        object.put("imageUrlPrefix","");
        object.put("imagePathFormat","/ueditor/jsp/upload/image/{yyyy}{mm}{dd}/{time}{rand:6}");
        return ok(object.toJSONString());
    }

    //上传处理函数
    public static Result ueditorUpload(){
        //图片上传
        if(getParam("action").equals("uploadimage")){
            Http.MultipartFormData formBody = request().body().asMultipartFormData();
            Http.MultipartFormData.FilePart filePart = formBody.getFile("upfile");
            String key =  new Date().getTime() + "_" + filePart.getFilename();
            ALiYunService.UploadResult result = ALiYunService.upload("chada", "framework/temp/", filePart.getFile(), key);
            JSONObject object = new JSONObject();
            object.put("url",result.url);
            object.put("title",filePart.getFilename());
            object.put("state","SUCCESS");
            return ok(object.toJSONString());
        }
        return ok("");
    }
    public static Result ueditorUploadFile(){
        return ok();
    }
}
